package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.central.service.ITopCentralProductTypeService;
import com.ruoyi.common.utils.service.ServiceUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralProductRelation;
import com.ruoyi.central.service.ITopCentralProductRelationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 产品关系Controller
 * 
 * @author ruoyi
 * @date 2022-03-02
 */
@Controller
@RequestMapping("/central/centralProductRelation")
public class TopCentralProductRelationController extends BaseController
{
    private String prefix = "central/centralProductRelation";

    @Autowired
    private ITopCentralProductRelationService topCentralProductRelationService;

    @Autowired
    private ITopCentralProductTypeService productTypeService;

    @RequiresPermissions("central:centralProductRelation:view")
    @GetMapping()
    public String centralProductRelation()
    {
        return prefix + "/centralProductRelation";
    }

    /**
     * 查询产品关系列表
     */
    @RequiresPermissions("central:centralProductRelation:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralProductRelation topCentralProductRelation)
    {
        startPage();
        List<TopCentralProductRelation> list = topCentralProductRelationService.selectList(topCentralProductRelation);

        ServiceUtils.complete(list,productTypeService,(productTypeMap,productRelation)->{
            productRelation.setProductType(productTypeMap.get(productRelation.getProductTypeId()));
            productRelation.setProductPartType(productTypeMap.get(productRelation.getProductPartTypeId()));
        });
        return getDataTable(list);
    }

    /**
     * 查询简单的产品关系列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralProductRelation topCentralProductRelation){

        Collection<TopCentralProductRelation> topCentralProductRelationList = topCentralProductRelationService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralProductRelationList,TopCentralProductRelation::getId,TopCentralProductRelation::getProductTypeId));
    }

    /**
     * 获取产品关系详细信息
     */
    @RequiresPermissions("central:centralProductRelation:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralProductRelationService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralProductRelation 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralProductRelation topCentralProductRelation){
        int weigth = topCentralProductRelationService.getMaxOfWeight(topCentralProductRelation);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载产品关系导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralProductRelation> util = new ExcelUtil<>(TopCentralProductRelation.class);
        util.importTemplateExcel(response, "产品关系数据");
    }

    /**
     * 导入产品关系数据
     * @param file 产品关系数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "产品关系", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralProductRelation:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralProductRelation> util = new ExcelUtil<>(TopCentralProductRelation.class);
        List<TopCentralProductRelation> topCentralProductRelationList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralProductRelationService.updateBatchById(topCentralProductRelationList)){
                return AjaxResult.success("导入产品关系数据覆盖成功！");
            }
            return AjaxResult.error("导入产品关系数据覆盖失败！");
        }
        if(topCentralProductRelationService.insertBatch(topCentralProductRelationList)){
            return AjaxResult.success("导入产品关系数据新增成功！");
        }
        return AjaxResult.error("导入产品关系数据新增失败！");
    }

    /**
     * 导出产品关系列表
     */
    @RequiresPermissions("central:centralProductRelation:export")
    @Log(title = "产品关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralProductRelation topCentralProductRelation)
    {
        List<TopCentralProductRelation> list = topCentralProductRelationService.selectList(topCentralProductRelation);
        ExcelUtil<TopCentralProductRelation> util = new ExcelUtil<TopCentralProductRelation>(TopCentralProductRelation.class);
        return util.exportExcel(list, "产品关系数据");
    }

    /**
     * 新增产品关系
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品关系
     */
    @RequiresPermissions("central:centralProductRelation:add")
    @Log(title = "产品关系", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralProductRelation topCentralProductRelation)
    {
        return toAjax(topCentralProductRelationService.insert(topCentralProductRelation));
    }

    /**
     * 修改产品关系
     */
    @RequiresPermissions("central:centralProductRelation:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralProductRelation topCentralProductRelation = topCentralProductRelationService.selectById(id);
        mmap.put("topCentralProductRelation", topCentralProductRelation);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品关系
     */
    @RequiresPermissions("central:centralProductRelation:edit")
    @Log(title = "产品关系", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralProductRelation topCentralProductRelation)
    {
        return toAjax(topCentralProductRelationService.updateById(topCentralProductRelation));
    }

    /**
     * 修改产品关系 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralProductRelation:edit")
    @Log(title = "产品关系", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralProductRelationService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 删除产品关系
     */
    @RequiresPermissions("central:centralProductRelation:remove")
    @Log(title = "产品关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralProductRelationService.deleteByIds(ids));
    }
}
